//
//  ViewController.m
//  CoreDataDemo
//
//  Created by 孙元东 on 2018/3/8.
//  Copyright © 2018年 MuJin. All rights reserved.
//

#import "ViewController.h"
#import "AppDelegate.h"
#import "SYDDataBase.h"
#define ENTITY_NAME @"Student"// 学生
#import "StudentCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, strong) AppDelegate *appdelegate;
@property (weak, nonatomic) IBOutlet UIView *headerView;

@end

@implementation ViewController
-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headerView.frame), self.view.frame.size.width, self.view.frame.size.height - 40 - 88) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
-(NSMutableArray *)dataList{
    if(!_dataList){
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     self.view.backgroundColor = [UIColor redColor];
    //拿到AppDelegate 对象
    self.appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
   
    //打开数据库
    [[SYDDataBase sharedSYDDataBase]openOrCreateSqlite:@"student"];
    //创建增删改查按钮
    
//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
    self.tableView.rowHeight = 100;
    //查询所有数据的请求
    [self.dataList removeAllObjects];
    NSArray *array = [[SYDDataBase sharedSYDDataBase]readDataFromEntityName:ENTITY_NAME];
    [self.dataList addObjectsFromArray:array];
    
}
- (IBAction)insertData:(UIButton *)sender {
    Person *person = [[Person alloc]init];
    person.name = [NSString stringWithFormat:@"Mr--%d",arc4random()%100];
    person.sex = arc4random()%2;
    person.age = arc4random()%30;
    [[SYDDataBase sharedSYDDataBase]insertDataFromEntityName:ENTITY_NAME model:person];
    
    //查询所有数据的请求
    NSArray *array = [[SYDDataBase sharedSYDDataBase]readDataFromEntityName:ENTITY_NAME];
    [self.dataList removeAllObjects];
    [self.dataList addObjectsFromArray:array];
    [self.tableView reloadData];
}
- (IBAction)deleteData:(UIButton *)sender {
    [[SYDDataBase sharedSYDDataBase]deleteDataFromEntityName:ENTITY_NAME];
    //查询所有数据的请求
    NSArray *array = [[SYDDataBase sharedSYDDataBase]readDataFromEntityName:ENTITY_NAME];
    [self.dataList removeAllObjects];
    [self.dataList addObjectsFromArray:array];
    [self.tableView reloadData];
}
- (IBAction)updateData:(UIButton *)sender {
    
    [[SYDDataBase sharedSYDDataBase]updateDataFromEntityName:ENTITY_NAME];
    //查询所有数据的请求
    NSArray *array = [[SYDDataBase sharedSYDDataBase]readDataFromEntityName:ENTITY_NAME];
    [self.dataList removeAllObjects];
    [self.dataList addObjectsFromArray:array];
    [self.tableView reloadData];
}
- (IBAction)searchData:(UIButton *)sender {
    NSArray *arr = [[SYDDataBase sharedSYDDataBase]getAllDataFromEntityName:ENTITY_NAME withCondition:@"0"];
    [self.dataList removeAllObjects];
    [self.dataList addObjectsFromArray:arr];
    [self.tableView reloadData];
}

- (IBAction)sortData:(UIButton *)sender {
    NSArray *arr = [[SYDDataBase sharedSYDDataBase]sortDataFromEntityName:ENTITY_NAME];
    [self.dataList removeAllObjects];
    [self.dataList addObjectsFromArray:arr];
    [self.tableView reloadData];
}
#pragma mark -- UITableDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //指定cell的重用标识符
    static NSString *reuseIdentifier = @"CELL";
    //去缓存池找名叫reuseIdentifier的cell
    StudentCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    //如果缓存池中没有,那么创建一个新的cell
    if (!cell) {
        //这里换成自己定义的cell,并调用类方法加载xib文件
        cell = [StudentCell xibTableViewCell];
    }
    Person *model = self.dataList[indexPath.row];
    [cell initModelWith:model];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
